﻿using BXH.Movie.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using Dapper;
namespace BXH.Movie.Dal
{
    public class MovieInfoDal : BaseConnection
    {
        public IEnumerable<MovieInfoDto> GetMovieInfos()
        {
            return Conn.Query<MovieInfoDto>(@"select m.*,t.MovieType as TypeName from MovieInfo as m
                                                                        left join MovieType as t on t.Id=m.Type");
        }
        public bool Add(MovieInfo movieInfo)
        {
            string sql = @"insert into MovieInfo (Name,Direct,Actor,Type,Country,Time,TicketPrice,Image,Remark,Brief) 
                                 values (@Name,@Direct,@Actor,@Type,@Country,@Time,@TicketPrice,@Image,@Remark,@Brief)";
            return Conn.Execute(sql, movieInfo) > 0;
        }
        public bool Update(MovieInfo movieInfo)
        {
            string sql = @"update  MovieInfo set 
                        Name=@Name,
                        Direct=@Direct,
                        Actor=@Actor,
                        Type=@Type,
                        Country=@Country,
                        Time=@Time,
                        TicketPrice=@TicketPrice,
                        Image=@Image,
                        Remark=@Remark 
                        where Id=@Id";
            return Conn.Execute(sql, movieInfo) > 0;
        }


        public bool Delete(int id)
        {
            string sql = @"delete from  MovieInfo 
                        where Id=@Id";
            string deleteSessionSql = @"delete from  MovieSession 
                        where  MovieId=@MovieId";
            Conn.Execute(deleteSessionSql, new { MovieId = id });
            return Conn.Execute(sql, new { Id = id }) > 0;
        }


        public IEnumerable<MovieType> GetAllMovieType()
        {
            return Conn.Query<MovieType>(@"select Id,MovieType as TypeName from MovieType");
        }
        public bool AddMovieType(MovieType movieType)
        {
            string sql = @"insert into MovieType (MovieType) 
                                 values (@TypeName)";
            return Conn.Execute(sql, movieType) > 0;
        }
        public bool UpdateMovieType(MovieType movieType)
        {
            string sql = @"update  MovieType set  MovieType=@TypeName where Id=@Id";
            return Conn.Execute(sql, movieType) > 0;
        }
        public bool DeleteMovieType(int id)
        {
            string sql = @"delete  from MovieType  where Id=@Id";
            return Conn.Execute(sql, new { Id = id }) > 0;
        }



    }
}
